<?php

class profileActions extends sfActions {

    public function executeIndex(sfWebRequest $request){
        //
    }

    public function executeShow(sfWebRequest $request){
        $id = $request->getParameter('id');
        $author = AccountPeer::retrieveByPK($id);
        if(is_object($author)){
            $this->author = $author;
        }else{
            $this->setTemplate('notFound');
        }
        $this->array_suattr = $this->getUser()->getAttribute('suattr');

        $c = new Criteria();
        $c->add(ArticlePeer::OWNER_ID, $id);
        $c->add(ArticlePeer::ENABLED, 1);
        $c->add(ArticlePeer::IS_DRAFT, 0);
        $this->articles = ArticlePeer::doSelect($c);

        /*crear metodo que reciba ids y devuelva arreglo de usuarios practil */
        $ids = array();
        $ids[] = $author->getPractilAccountId();

        $myLib = new myLib();
        $array_users = array();
        $array_users = $myLib->getUsersFromPractil($ids);

        $this->array_users = $array_users;

        $recommended = false;
        $c->clear();
        $c->add(RecommendationPeer::RECOMMENDER_ID, $this->getUser()->getAttribute('suid'));
        $c->add(RecommendationPeer::INVITED_ID, $id);
        $recommendation = RecommendationPeer::doSelectOne($c);
        if(is_object($recommendation)){
            $recommended = true;
        }
        $this->recommended = $recommended;
    }

    public function executeRecommendForAuthor(sfWebRequest $request){
        $id = $request->getParameter('id');
        $author = AccountPeer::retrieveByPK($id);

        $ids = array();
        $ids[] = $author->getPractilAccountId();

        $myLib = new myLib();
        $array_users = array();
        $array_users = $myLib->getUsersFromPractil($ids);

        $recommend = new Recommendation();
        $recommend->setInvitedId($id);
        $recommend->setRecommenderId($this->getUser()->getAttribute('suid'));
        $recommend->setCreatedAt(date("Y-m-d H:i:s"));
        $recommend->save();

        /* verifica si ya se le ha enviado una invitación, en caso no se haya realizado entonces se envía */
        $c = new Criteria();
        $c->add(InvitationPeer::INVITED_ID, $id);
        $invitation = InvitationPeer::doSelectOne($c);
        if(!is_object($invitation)){
            $invitation = new Invitation();
            $invitation->setInvitedId($id);
            $invitation->setRecommenderId($this->getUser()->getAttribute('suid'));
            $invitation->setState(1);/* pendiente */
            $invitation->setCreatedAt(date("Y-m-d H:i:s"));
            $invitation->save();

            //correo
            $message = $this->getMailer()->compose();
            $message->setSubject('(Opinics) Invitación para escritor');
            $message->setTo($array_users[0]->{'email'});
            $message->setFrom(array('mailing@practil.com' => 'Practil Mailing'));
            $html = $this->getPartial('mailing/invitation', array(
                'user'=>$array_users[0]
            ));
            $message->setBody($html, 'text/html');
            $this->getMailer()->send($message);
        }

        $this->user = $array_users[0];
    }

}